#PBAR_Zspec_ExamineData.py
#
#  Exploratory plots for Zspec material discrimination datasets.
#
#
#
#  4/1/2013, John Kwong
#  5/1/2013, removed plots related to material discrimination.

# LOAD STATS
#stats = PBAR_Zspec.LoadStats(basepath, filenameListStats)


#####################
##  Spectra plots  ##
#####################

# SPECTRA for multiple datasets, single detector
detector = 88
detector = 59

# plotList = plotList_CollimatorClosed
plotList = (datasetGroupsIndices['Pb'], datasetGroupsIndices['Fe'], datasetGroupsIndices['Al'])
#plotList = (datasetGroupsIndices['Pb'], datasetGroupsIndices['PbLS'])
#plotList = (datasetGroupsIndices['Fe'], datasetGroupsIndices['FeLS'])
#plotList = (datasetGroupsIndices['Al'], datasetGroupsIndices['AlLS'])
#plotList = (datasetGroupsIndices['Pb5_8'], datasetGroupsIndices['PbLS'])
##plotList = (datasetGroupsIndices['Fe12'], datasetGroupsIndices['FeLS'])
##plotList = (datasetGroupsIndices['Al30'], datasetGroupsIndices['AlLS'])

correctGain = False
plotRate = True;
fig = plt.figure()
kk = 0
for jj in range(len(plotList)):
    subPlotList = plotList[jj]
    for ii in range(0,len(subPlotList)):
        
        print jj, ii
        pp = subPlotList[ii]
        bins = np.arange(0,256)
        if correctGain:
            correction1 = gainExtrapolated[0,detector] / gainExtrapolated[pp,detector]    
        else:
            correction1 = 1.0
        correctionMat = np.matlib.repmat(1, 1, 256)[0]
        correctionMat[0:8] = 0
        if plotRate:
            plt.plot(bins * correction1, dat[pp][:,detector]/datasetAcquisitionTime[pp], \
                     ls = lineStyles[kk/7], color = plotColors[kk%7], \
                     label = (filenameList[pp]+ ', ' + datasetDescription[pp] + ', ' + datasetTimeStr[pp]))
        else:
            plt.plot(bins * correction1, dat[pp][:,detector], \
                     ls = lineStyles[kk/7], color = plotColors[kk%7], \
                     label = (filenameList[pp]+ ', ' + datasetDescription[pp] + ', ' + datasetTimeStr[pp]))
        kk = kk + 1
            
##plt.plot(np.array([0, 255, 255, 0, 0]), np.array([countRange[0], countRange[0], countRange[1], countRange[1], countRange[0]]))

if correctGain:
    plt.title('Detector #' + str(detector+1) + ', Gain Corrected')
else:
    plt.title('Detector #' + str(detector+1))
plt.legend(prop={'size':10})
plt.yscale('log')
##plt.yscale('linear')
plt.xlabel('Bin', fontsize = 16)

if plotRate:
    plt.ylabel('Rate', fontsize = 16)
else:
    plt.ylabel('Count', fontsize = 16)
plt.grid()
plt.xlim((0,250))



# SPECTRA for multiple datasets, single detector, MONTHLY REPORT
detector = 88
detector = 68

# plotList = plotList_CollimatorClosed
plotList = (datasetGroupsIndices['Pb'], datasetGroupsIndices['Fe'], datasetGroupsIndices['Al'])

correctGain = 0

fig = plt.figure()

for jj in range(len(plotList)):
    subPlotList = plotList[jj]
    for ii in range(0,len(subPlotList)):

        if ((jj == 0 and ii == 0) or (jj == 1 and ii == 0) or (jj == 2 and ii == 3)):
            print jj, ii
            pp = subPlotList[ii]
            bins = np.arange(0,256)
            if correctGain:
                correction1 = gainExtrapolated[0,detector] / gainExtrapolated[pp,detector]    
            else:
                correction1 = 1.0
            correctionMat = np.matlib.repmat(1, 1, 256)[0]
            correctionMat[0:8] = 0
            
            plt.plot(bins * correction1, dat[pp][:,detector], \
                     ls = lineStyles[jj%7], color = plotColors[0], label = (filenameList[pp]+ ', ' + datasetDescription[pp] + ', ' + datasetTimeStr[pp]))

if correctGain:
    plt.title('Detector #' + str(detector+1) + ', Gain Corrected')
else:
    plt.title('Detector #' + str(detector+1))

plt.legend(prop={'size':10})
plt.yscale('log')
##plt.yscale('linear')
plt.xlabel('Bin', fontsize = 16)
plt.ylabel('Counts', fontsize = 16)
plt.grid()
plt.xlim((0,250))


# PLOT MULTIPLE SPECTRA FOR A SINGLE GROUP

# plotList = plotList_CollimatorClosed
plotList = (datasetGroupsIndices['Pb'], datasetGroupsIndices['Fe'], datasetGroupsIndices['Al'])
plotList = (datasetGroupsIndices['Pb'], datasetGroupsIndices['PbLS'])
plotList = (datasetGroupsIndices['Fe'], datasetGroupsIndices['FeLS'])
plotList = (datasetGroupsIndices['Al'], datasetGroupsIndices['AlLS'])

pp = datasetGroupsIndices['CC'][0]
##plotList = (datasetGroupsIndices['Fe12'], datasetGroupsIndices['FeLS'])
##plotList = (datasetGroupsIndices['Al30'], datasetGroupsIndices['AlLS'])

fig = plt.figure()
kk = 0
for ii in np.arange(0, len(goodDetectorsList), 10):
    jj = goodDetectorsList[ii]
    plt.plot(np.arange(256), dat[pp][:,jj], \
             ls = lineStyles[kk/7], \
             color = plotColors[kk%7], \
             label = str(jj))
    kk = kk + 1

plt.title(  'Detector #' + str(detector+1)  )
plt.legend(prop={'size':10})
plt.yscale('log')
##plt.yscale('linear')
plt.xlabel('Bin', fontsize = 16)

if plotRate:
    plt.ylabel('Rate', fontsize = 16)
else:
    plt.ylabel('Count', fontsize = 16)
plt.grid()
plt.xlim((0,250))


# PLOT SPECTRUM WITH RATE CORRECTION
detector = 45

##plotList = plotList_Fe_Good
##transmissionCorrection = transmissionCorrectionFe_Good
##
##plotList = plotList_Al
##transmissionCorrection = transmissionCorrectionAl

# plotList = plotList_CollimatorClosed

plotList = plotList_Pb_Good
transmissionCorrection = transmissionCorrectionPb_Good

jj = 0;

fig = plt.figure()
for ii in range(0,len(plotList)):
    pp = plotList[ii]
    x = dat[pp][:,detector]/datasetAcquisitionTime[pp]*transmissionCorrection[ii]
    plt.plot(x, ls = lineStyles[jj/7], label = (filenameList[pp]+ ', ' + datasetDescription[pp] + ', ' + datasetTime[pp]))
    jj = jj + 1;
    
    
plotList = plotList_Fe_Good
transmissionCorrection = transmissionCorrectionPb_Good

for ii in range(0,len(plotList)):
    pp = plotList[ii]
    x = dat[pp][:,detector]/datasetAcquisitionTime[pp]*transmissionCorrection[ii]
    plt.plot(x, ls = lineStyles[jj/7], label = (filenameList[pp]+ ', ' + datasetDescription[pp] + ', ' + datasetTime[pp]))
    jj = jj + 1
    
plt.title('Detector #' + str(detector+1))
plt.legend()
plt.yscale('log')
plt.xlabel('Bin', fontsize = 16)
plt.ylabel('Rate', fontsize = 16)
plt.grid()


##################
##  STAT PLOTS  ##
##################

# STAT AS A FUNCTION OF ZSPEC DETECTOR NUMBER

plotList = datasetGroupsIndices['Pb']
plotList = datasetGroupsIndices['CC']

statName1 = 'multibin_20_ratio_g'
statName1 = 'binMean_g1'
statName1 = 'binMean'
fig = plt.figure()
for ii in range(0,len(plotList)):
    pp = plotList[ii]
    plt.plot(np.arange(stats[statName1][pp,:].shape[0])+1, stats[statName1][pp,:], \
             marker = markerTypes[ii], label = (datasetTimeStr[pp]))

plt.title(statName1 + ' Versus Detector Number')
plt.legend(loc = 4, prop={'size':10})
plt.xlabel('Detector No.', fontsize = 12)
plt.ylabel(statName1, fontsize = 12)
grid()


# STAT AS A FUNCTION OF CC GAIN VARIABLE for one CC dataset

plotList = datasetGroupsIndices['Pb']
plotList = datasetGroupsIndices['CC']

##statName1 = 'multibin_20_ratio_g'
statName1 = 'binMean_g'

fig = plt.figure()
cutt = goodDetectorsList
for ii in range(1):
    pp = plotList[ii]
    plt.scatter(gainExtrapolated[pp,cutt].T, stats[statName1][pp,cutt].T, \
             marker = markerTypes[ii], label = (datasetTimeStr[pp]))

plt.title(statName1 + 'Versus CC Calibration Bin')
plt.legend(loc = 4, prop={'size':10})
plt.xlabel('CC Calibration Bin', fontsize = 12)
plt.ylabel(statName1, fontsize = 12)
grid()


# GAIN SHIFT AS A FUNCTION OF ZSPEC DETECTOR NUMBER
plotList = datasetGroupsIndices['Pb']
plotList = datasetGroupsIndices['CC']

fig = plt.figure()
for ii in range(0,len(plotList)):
    pp = plotList[ii]
##    plt.plot(np.arange(1,138), gainExtrapolated[pp,:]/gainExtrapolated[plotList[0],:], \
##             marker = markerTypes[ii], label = (filenameList[pp]+ ', ' + datasetDescription[pp] + ', ' + datasetTimeStr[pp]))
    plt.plot(np.arange(1,137), gainExtrapolated[pp,:]/gainExtrapolated[plotList[0],:], \
             marker = markerTypes[ii], label = (datasetTimeStr[pp]))

plt.title('Gain Shift')
plt.legend(loc = 4, prop={'size':10})
plt.xlabel('Detector No.', fontsize = 12)
plt.ylabel('Gain Shift', fontsize = 12)
grid()

# PLOT GAINSHIFT VERSUS TIME
detectorList = np.arange(60,70)
plotList = datasetGroupsIndices['CC']
fig = plt.figure()
timeArray = datasetTimeNum[plotList]

for ii in range(0,len(plotList)):
    detector = detectorList[ii]
    gainShift = gainExtrapolated[plotList,detector]/gainExtrapolated[plotList[0],detector]
    plt.plot((timeArray - timeArray[0])/3600, gainShift, marker = markerTypes[0], label = str(detector))

plt.title('Gain Shift Vs Time, t_0 = ' + datasetTimeStr[plotList[0]])
plt.legend(loc = 2, prop={'size':10})
plt.xlabel('Time (Hours)', fontsize = 16)
plt.ylabel('Gain Shift', fontsize = 16)
grid()

